<template>
  <view class="goodsList">
    <view class="title">商品列表</view>
    <view class="goods">
      <view v-for="(i, index) in temporary" :key="index">
        <view class="selectGoods" v-if="i.hasOwnProperty('checked')">
          <checkbox :disabled="preview" :checked="i.checked" @click="clickChecked(i)" />
          <view class="right" v-if="!preview">
            <text class="reduce" @click="reduce(i)">-</text>
            <text class="inputNum">{{ i.nums }}</text>
            <text class="add" @click="add(i)">+</text>
          </view>
          <view class="right" v-else>
            <text class="reduce preview">-</text>
            <text class="inputNum preview">{{ i.nums }}</text>
            <text class="add preview">+</text>
          </view>
        </view>

        <view class="recommend_item">
          <!-- <image class="original" :src="i.image ? i.image : staticUrl() + '/home/defaultImg.png'" /> -->
          <view class="original">
            <ImageCache :src="i.image ? i.image : staticUrl() + '/home/defaultImg.png'" />
          </view>
          <view style="width: 54%">
            <view class="goodsName">
              <text class="singleRowEllipsis">{{ i.goodsName }}</text>
              <text class="num" v-if="!i.hasOwnProperty('checked')">x{{ i.num }}</text>
            </view>
            <view class="simpleSpecs singleRowEllipsis">规格：{{ i.simpleSpecs || '' }}</view>
            <view class="expirationDate">
              <img :src="staticUrl() + '/home/expirationDate.png'" alt="" class="_icon" />
              <text>{{ i.expirationDate || '' }}</text>
            </view>
            <view class="manufacturer singleRowEllipsis">
              <img :src="staticUrl() + '/home/manufacturer.png'" alt="" class="_icon" />
              <text>{{ i.manufacturer || '' }}</text>
            </view>
            <!-- <view class="activityType" v-if="i.promotion"> {{i.promotion.promotionName}} </view> -->
            <view class="manufacturer">
              <span class="ephedrine" v-if="i.ephedrine">含麻</span>
              <text v-for="(n, m) in i.promotionMethods" :key="m"
                :class="n.promotionType === 'COUPON' ? 'voucher' : 'seckillType'">
                {{ n.promotionType === 'COUPON' ? '券' : n.name }}
              </text>
            </view>
            <view class="prices">
              <view class="priceRow">
                <text>￥</text>
                {{ i.goodsPrice }}
              </view>
              <text class="num"
                v-if="i.outboundNum && !i.hasOwnProperty('checked')">出库:{{ i.outboundNum }}</text>
              <text class="num"
                v-if="i.outboundNum && i.hasOwnProperty('checked')">x{{ i.num }}</text>
            </view>
          </view>
        </view>
      </view>
      <view class="more" @click="more" v-if="orderItem.length > 2">
        <text>{{ openArrowText }}</text>
        <image :src="staticUrl() + openArrow" mode="aspectFit"></image>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  props: {
    temporary_list: {
      type: Array,
      default: []
    },
    orderItems: {
      type: Array,
      default: []
    },
    preview: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      temporary: this.temporary_list,
      orderItem: this.orderItems,
      openArrowText: '全部打开',
      openArrow: '/carts/moreRule.png'
    }
  },
  methods: {
    // 查看更多
    more () {
      if (this.openArrowText === '收起') {
        this.openArrowText = '全部打开'
        this.openArrow = '/carts/moreRule.png'
        this.temporary = this.orderItem.slice(0, 2)
      } else {
        this.openArrowText = '收起'
        this.openArrow = '/carts/putAway.png'
        this.temporary = JSON.parse(JSON.stringify(this.orderItem))
      }
    },
    //
    clickChecked (i) {
      if (i.promotionType === 'PORTFOLIO') {
        this.temporary.forEach(j => {
          if (i.portfolioSkuId === j.portfolioSkuId) j.checked = !j.checked
        })
        this.orderItem.forEach(k => {
          if (i.portfolioSkuId === k.portfolioSkuId) k.checked = !k.checked
        })
        if (i.checked) {
          this.add(i)
        } else {
          this.reduce(i)
        }
      } else {
        this.temporary.forEach(j => {
          if (i.skuId === j.skuId) j.checked = !j.checked
        })
        this.orderItem.forEach(k => {
          if (i.skuId === k.skuId) k.checked = !k.checked
        })
        if (i.checked) {
          this.temporary.forEach(item => {
            if (item.identifier === i.identifier || item.skuId === i.skuId) {
              i.nums = i.num
              item.nums = item.num
              item.checked = true
            }
          })
          this.orderItem.forEach(item => {
            if (item.identifier === i.identifier || item.skuId === i.skuId) {
              i.nums = i.num
              item.nums = item.num
              item.checked = true
            }
          })
        } else {
          this.temporary.forEach(item => {
            if (item.identifier === i.identifier || item.skuId === i.skuId) {
              i.nums = 0
              item.nums = 0
              item.checked = false
            }
          })
          this.orderItem.forEach(item => {
            if (item.identifier === i.identifier || item.skuId === i.skuId) {
              i.nums = 0
              item.nums = 0
              item.checked = false
            }
          })
        }
      }
      this.$emit('updateGoodsList', this.orderItem)
    },
    add (i) {
      if (i.promotionType === 'DEDICATED' || i.promotionType === 'FREE_SHIPPING') {
        this.temporary.forEach(item => {
          if (item.identifier === i.identifier) {
            i.nums = i.num
            item.nums = item.num
            item.checked = true
          }
        })
      } else if (i.promotionType === 'PORTFOLIO') {
        this.temporary.forEach(item => {
          if (item.portfolioSkuId === i.portfolioSkuId) {
            i.nums = i.num
            item.nums = item.num
            item.checked = true
          }
        })
      } else {
        i.num > i.nums ? (i.nums += i.pack) : ''
        i.checked = true
      }
      i.num > i.nums ? '' : (i.checked = true)
    },
    reduce (i) {
      if (i.promotionType === 'DEDICATED' || i.promotionType === 'FREE_SHIPPING') {
        this.temporary.forEach(item => {
          if (item.identifier === i.identifier) {
            i.nums = 0
            item.nums = 0
            item.checked = false
          }
        })
      } else if (i.promotionType === 'PORTFOLIO') {
        this.temporary.forEach(item => {
          if (item.portfolioSkuId === i.portfolioSkuId) {
            i.nums = 0
            item.nums = 0
            item.checked = false
          }
        })
      } else {
        i.nums > 0 ? (i.nums -= i.pack) : (i.checked = false)
      }
      i.nums > 0 ? '' : (i.checked = false)
    }
  }
}
</script>

<style lang="scss" scoped>
.goodsList {
  background: #fff;
  margin: 0 25rpx;
  border-radius: 11rpx;

  .title {
    font-size: 29rpx;
    font-weight: bold;
    padding: 38.19rpx 0 20.14rpx 22.22rpx;
    border-bottom: 1px solid #f5f5f5;
  }

  .goods {
    .selectGoods {
      display: flex;
      justify-content: space-between;
      height: 100rpx;
      align-items: center;
      padding: 0 25rpx;

      .right {
        text-align: center;

        .reduce {
          display: inline-block;
          width: 43.06rpx;
          height: 43.06rpx;
          border: 1px solid #2871fc;
          line-height: 36rpx;
          border-radius: 10rpx;
          margin-right: 10rpx;
        }

        .inputNum {
          padding: 7rpx 15rpx;
          background: #2871fc;
          font-size: 26rpx;
          color: #fff;
          margin-right: 10rpx;
        }

        .add {
          display: inline-block;
          width: 43.06rpx;
          height: 43.06rpx;
          border: 1px solid #2871fc;
          line-height: 38rpx;
          text-align: center;
          border-radius: 10rpx;
        }

        .preview {
          color: #8d8d8d;
          background: #f9f9f9;
          border: 0;
        }
      }
    }

    .recommend_item {
      display: flex;
      border-bottom: 1px solid #f5f5f5;

      .num {
        color: #8d8d8d;
        float: right;
        font-size: 25rpx;
      }

      .original {
        width: 194rpx;
        height: 194rpx;
        background: #ffffff;
        padding: 34.72rpx 21rpx;
        margin: 44.44rpx 59.03rpx 108.33rpx 37.5rpx;
        box-sizing: border-box;
      }

      .goodsName {
        font-size: 29rpx;
        margin-top: 38.19rpx;
        margin-bottom: 19.44rpx;
        display: flex;
        justify-content: space-between;

        text:nth-child(1) {
          width: 80%;
        }
      }

      .simpleSpecs {
        font-size: 21rpx;
        margin-bottom: 14.58rpx;
        color: #8d8d8d;
      }

      .expirationDate {
        font-size: 21rpx;
        margin-bottom: 14.58rpx;
        color: #8d8d8d;
      }

      ._icon {
        width: 20rpx;
        height: 20rpx;
        vertical-align: middle;
        margin-right: 7.64rpx;
      }

      .manufacturer {
        font-size: 20rpx;
        margin-bottom: 14.58rpx;
        color: #8d8d8d;
      }

      .activityType {
        display: inline-block;
        font-size: 19rpx;
        color: #ef231b;
        border: 1px solid #ef231b;
        padding: 0 8.6rpx;
        border-radius: 4rpx;
        margin-top: 13.89rpx;
      }

      .prices {
        display: flex;
        justify-content: space-between;
        align-items: flex-end;
        margin-top: 25rpx;

        .priceRow {
          color: #ec1818;
          font-weight: bold;
          font-size: 30rpx;

          text {
            font-size: 25rpx;
            font-weight: normal;
            // color: #8d8d8d;
            margin-left: 6.94rpx;
          }
        }
      }
    }

    .more {
      display: flex;
      justify-content: center;
      align-items: center;
      padding: 27.38rpx 0;

      text {
        font-size: 29rpx;
        font-weight: bold;
      }

      image {
        max-width: 16.67rpx;
        height: 11.11rpx;
        margin-left: 12.5rpx;
      }
    }
  }
}
</style>
